package com.gg.registration.hospital.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gg.registration.hospital.repository.DepartmentRepository;
import com.gg.registration.hospital.service.DepartmentService;
import com.gg.registration.model.hosp.Department;
import com.gg.registration.model.hosp.Hospital;
import com.gg.registration.vo.hosp.DepartmentQueryVo;
import com.gg.registration.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chenjunjia
 * @date 2022/4/9 14:31
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Resource
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {

        //使用ObjectMapper转对象吧
        ObjectMapper om = new ObjectMapper();
        Department department = om.convertValue(paramMap, Department.class);
        Department departmentResult = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(),department.getDepcode());

        //如果存在，进行修改
        if(departmentResult != null) {
            departmentResult.setUpdateTime(new Date());
            departmentResult.setIsDeleted(0);
            departmentRepository.save(departmentResult);
        } else {
            //如果不存在，进行添加
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    /**
     * 分页查询科室信息
     * @param page 当前页
     * @param limit 每页显示n条
     * @param departmentQueryVo 数据对象
     * @return 分页对象
     */
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {

        //0是第一页
        Pageable pageable = PageRequest.of(page-1,limit);
        // 创建Example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);

        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department,matcher);

        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;
    }

    /**
     * 删除科室接口
     * @param hoscode 医院编号
     * @param depcode 科室编号
     */
    @Override
    public void remove(String hoscode, String depcode) {
        //根据医院编号和科室编号查询
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            departmentRepository.deleteById(department.getId());
            HashMap hashMap = new HashMap();
            Set set = hashMap.entrySet();

        }
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }
    //根据科室编号，和医院编号，查询科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }

    @Override
    public List<DepartmentVo> getDepartmentTree(String hoscode) {
        //在这个List统一返回，用来封装大科室
        List<DepartmentVo> result = new ArrayList();
        //装载条件专用对象
        Department var = new Department();
        var.setHoscode(hoscode);
        Example<Department> example = Example.of(var);
        //根据医院编号 查出所有记录
        List<Department> departmentAll = departmentRepository.findAll(example);
        //用Stream流来根据科室code分组封装
        //Map的key代表所有大科室的科室code，也就是Department::getBigcode
        Map<String, List<Department>> collect = departmentAll.stream().collect(Collectors.groupingBy(Department::getBigcode));
        Set<Map.Entry<String, List<Department>>> entries = collect.entrySet();
        //遍历集合，完成大小科室的封装
        for (Map.Entry<String, List<Department>> entry : entries){
            //封装小科室的List
            List<DepartmentVo> children = new ArrayList<>();
            //大科室编号对应的小科室List
            List<Department> departmentList1 = entry.getValue();
            //封装大科室操作
            DepartmentVo big = new DepartmentVo();
            big.setDepcode(entry.getKey());
            big.setDepname(departmentList1.get(0).getBigname());
            //遍历大科室编号对应的小科室集合，完成小科室封装
            for (Department department : departmentList1){
                DepartmentVo smallDepartment = new DepartmentVo();
                smallDepartment.setDepcode(department.getDepcode());
                smallDepartment.setDepname(department.getDepname());
                children.add(smallDepartment);
            }
            big.setChildren(children);
            result.add(big);
        }
        return result;
    }

}
